////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//  Filename:    VikingsVoyageGuts.cpp                                        //
//  Developer:   Tommy Riddle                                                 //
//  Create date: 03/18/2005                                                   //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//  Description: This is the class implementation for the slot gut of the     //
//               Australian Slot machine called Viking's Voyage.              //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
//        Copyright (c) 2005 - Realtime Gaming.  All rights reserved.         //
////////////////////////////////////////////////////////////////////////////////

#include "VikingsVoyageGuts.h"

////////////////////////////////////////////////////////////////////////////
/// VikingsVoyageGuts::m_aLineWinSpinResults
///
/// This array is used to quickly get the SpinResult for the number of
/// icons.  It is indexed by wheel icon and then count.  So to get the
/// SpinResult for 5 Danes, use the following code snippet:
///
///		SpinResult result = m_aLineWinSpinResults[ ICON_DANE ][ 5 ];
///
/// In this example result would be set to EVT_FIVE_DANE.
////////////////////////////////////////////////////////////////////////////
VikingsVoyageGuts::SpinResult VikingsVoyageGuts::m_aLineWinSpinResults[NUM_VALID_ICONS][6] = 
{
// Number of icons:		  0				1			2				3					4					5
/* ICON_NINE */			{ EVT_LOSER,	EVT_LOSER,	EVT_LOSER,		EVT_THREE_NINE,		EVT_FOUR_NINE,		EVT_FIVE_NINE	},
/* ICON_TEN */			{ EVT_LOSER,	EVT_LOSER,	EVT_LOSER,		EVT_THREE_TEN,		EVT_FOUR_TEN,		EVT_FIVE_TEN	},
/* ICON_JACK */			{ EVT_LOSER,	EVT_LOSER,	EVT_LOSER,		EVT_THREE_JACK,		EVT_FOUR_JACK,		EVT_FIVE_JACK	},
/* ICON_QUEEN */		{ EVT_LOSER,	EVT_LOSER,	EVT_LOSER,		EVT_THREE_QUEEN,	EVT_FOUR_QUEEN,		EVT_FIVE_QUEEN	},
/* ICON_KING */			{ EVT_LOSER,	EVT_LOSER,	EVT_LOSER,		EVT_THREE_KING,		EVT_FOUR_KING,		EVT_FIVE_KING	},
/* ICON_ACE */			{ EVT_LOSER,	EVT_LOSER,	EVT_LOSER,		EVT_THREE_ACE,		EVT_FOUR_ACE,		EVT_FIVE_ACE	},
/* ICON_GOBLET */		{ EVT_LOSER,	EVT_LOSER,	EVT_TWO_GOBLET,	EVT_THREE_GOBLET,	EVT_FOUR_GOBLET,	EVT_FIVE_GOBLET	},
/* ICON_GOLD */			{ EVT_LOSER,	EVT_LOSER,	EVT_TWO_GOLD,	EVT_THREE_GOLD,		EVT_FOUR_GOLD,		EVT_FIVE_GOLD	},
/* ICON_AXE */			{ EVT_LOSER,	EVT_LOSER,	EVT_TWO_AXE,	EVT_THREE_AXE,		EVT_FOUR_AXE,		EVT_FIVE_AXE	},
/* ICON_DANE */			{ EVT_LOSER,	EVT_LOSER,	EVT_TWO_DANE,	EVT_THREE_DANE,		EVT_FOUR_DANE,		EVT_FIVE_DANE	},
/* ICON_LONGBOAT */		{ EVT_LOSER,	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,			EVT_LOSER,			EVT_LOSER		},
/* ICON_VIKING */		{ EVT_LOSER,	EVT_LOSER,	EVT_LOSER,		EVT_LOSER,			EVT_LOSER,			EVT_LOSER		}
};

////////////////////////////////////////////////////////////////////////////
/// VikingsVoyageGuts::m_aScatterWinSpinResults
///
/// This array is used to quickly get the SpinResult for the number of
/// icons.  It is indexed by game mode and then icon count only, since
/// Longboats are the only scatter win.  It has to be indexed by game mode
/// because there is a payout for 2 Longboats in the feature games.  So to
/// get the SpinResult for 5 Longboats in the normal game mode, use the
/// following code snippet:
///
///		SpinResult result = m_aScatterWinSpinResults[ MODE_NORMAL ][ 5 ];
///
/// In this example result would be set to EVT_FIVE_LONGBOAT.
////////////////////////////////////////////////////////////////////////////
VikingsVoyageGuts::SpinResult VikingsVoyageGuts::m_aScatterWinSpinResults[NUM_GAME_MODES][6] = 
{
// Number of icons:		  0				1						2						3					4					5
/* MODE_NORMAL */		{ EVT_LOSER,	EVT_SCATTER_NEAR_MISS,	EVT_SCATTER_NEAR_MISS,	EVT_THREE_LONGBOAT,	EVT_FOUR_LONGBOAT,	EVT_FIVE_LONGBOAT },
/* MODE_FREE_SPIN */	{ EVT_LOSER,	EVT_SCATTER_NEAR_MISS,	EVT_TWO_LONGBOAT,		EVT_THREE_LONGBOAT,	EVT_FOUR_LONGBOAT,	EVT_FIVE_LONGBOAT },
/* MODE_RESPIN */		{ EVT_LOSER,	EVT_SCATTER_NEAR_MISS,	EVT_TWO_LONGBOAT,		EVT_THREE_LONGBOAT,	EVT_FOUR_LONGBOAT,	EVT_FIVE_LONGBOAT }
};
        
////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::VikingsVoyageGuts()   
///
/// This is the default constructor for the class.
////////////////////////////////////////////////////////////////////////////
VikingsVoyageGuts::VikingsVoyageGuts()
{
	// If this doesn't compile it means that someone probably added
	// another icon type but did not update the
	// m_aLineWinSpinResults array. 
	C_ASSERT( countof( m_aLineWinSpinResults ) == NUM_VALID_ICONS );

	// If this doesn't compile it means that someone probably added
	// another game mode but did not update the
	// m_aScatterWinSpinResults array. 
	C_ASSERT( countof( m_aScatterWinSpinResults ) == NUM_GAME_MODES );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::~VikingsVoyageGuts()
///
/// Destructor for VikingsVoyageGuts. 
////////////////////////////////////////////////////////////////////////////
VikingsVoyageGuts::~VikingsVoyageGuts()
{
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::SpinResult VikingsVoyageGuts::getLineWinResultWithPossibleSubstitution(	uint8* stops, uint8 testIcon, int nDirection, bool& bVikingSubstitution ) const
///
/// @param   stops					The array of stop positions for the line.
/// @param   et						The direction that we should use when we evaluate the line.
/// @param   bVikingSubstitution	An output parameter that is set if a subsitution is used.
///
/// @return	 SpinResult				A payout from the SpinResult enumeration.
///
/// This helper method is used to calculate the line win payout with a possible substitution.
////////////////////////////////////////////////////////////////////////////
VikingsVoyageGuts::SpinResult VikingsVoyageGuts::getLineWinResultWithPossibleSubstitution(
	uint8* stops, EvalType et, bool& bVikingSubstitution ) const
{
	// Initialize the out parameter
	bVikingSubstitution = false;

	// Keep up with the direction we are going.
	int nDirection;

	// We need a place to keep up with the starting reel since we can go
	// in either direction.
	int nStartReel;

	// If we are going from left to right...
	if ( et == etLeftToRight )
	{
		// then start at reel 0.
		nStartReel = 0;

		// We will need to increment the reel as we iterate through the loop.
		nDirection = 1;
	}
	else
	{
		// Otherwise we must be going from right to left.
		ASSERT( et == etRightToLeft );

		// Start on the last reel.
		nStartReel = getReelCount() - 1;

		// We will need to decrement the reel as we iterate through the loop.
		nDirection = -1;
	}

	// The icon we are testing for.
	uint8 testIcon = getIcon( getGameModeId(), nStartReel, stops[ nStartReel ] )->iconid;;

	// Loop through the remaining reels until the streak is over
	for ( int i = 1; i < getReelCount(); i++ )
    {
		// Figure out what our current reel is for this iteration.
		int nCurrentReel = nStartReel + ( i * nDirection );

		// See what this icon is.
		uint8 currentIcon = getIcon( getGameModeId(), nCurrentReel, stops[ nCurrentReel ] )->iconid;

		// If the current icon is a Viking...
		if ( currentIcon == ICON_VIKING )
		{
			// then use it as a substitution symbol and continue on.
			bVikingSubstitution = true;
			continue;
		}
		// Otherwise if the test icon is a Viking...
		else if ( testIcon == ICON_VIKING )
		{
			// then we still have a Viking substitution.  In this case the line
			// started out with a Viking.
			bVikingSubstitution = true;

			// We have now found our first icon that is not a Viking.  All
			// of the rest of the icons will have to match this one now for the
			// streak to continue, so change our test icon.
			testIcon = currentIcon;
			continue;
		}

		// If the current icon is not the test icon...
        if ( currentIcon != testIcon )
        {
			// then the streak is over so break out of the loop
			break;
        }
    }

	// Use the array to look up the spin result for i testIcons.
	return lookupLineWinSpinResult( testIcon, i );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 bool VikingsVoyageGuts::doesRightToLeftLineWinPayMore( SpinResult srLeftToRight, SpinResult srRightToLeft ) const;
///
/// @param   srLeftToRight	The spin result to calculate the left to right payout for.
/// @param   srRightToLeft	The spin result to calculate the right to left payout for.
///
/// @return	 bool			True if the right to left win is larger.
///
/// This helper method is used to see which payout is larger.
////////////////////////////////////////////////////////////////////////////
bool VikingsVoyageGuts::doesRightToLeftLineWinPayMore( SpinResult srLeftToRight,
	SpinResult srRightToLeft ) const
{
	// Lookup the payout for the left to right line win.
	long nLeftToRightLineWinPayout = getPayout( srLeftToRight )->payoutAmount;

	// Lookup the payout for the right to left line win.
	long nRightToLeftLineWinPayout = getPayout( srRightToLeft )->payoutAmount;

	// If the payout is the same for both directions...
	if ( nRightToLeftLineWinPayout == nLeftToRightLineWinPayout )
	{
		// then return true if the right to left spin result is higher.
		return ( srRightToLeft > srLeftToRight );
	}

	// Return true if the right to left payout is bigger.
    return ( nRightToLeftLineWinPayout > nLeftToRightLineWinPayout );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::classifyStops()   
///
/// @param   stops      The array of stop positions for the line.
/// @param   bet        The value of the line bet.
/// @param   line       The index of the line.
///
/// @return	 uint8      A payout from the SpinResult enumeration as a uint8.    
///
/// This helper method is used to calculate the payout on a line.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::classifyStops( SpinLine* spinline, uint8* stops, uint8 bet, uint8 line,
	uint8& rtnresult, uint8& multiplier )
{
	// If we are in re-spin mode...
	if ( getGameModeId() == MODE_RESPIN )
	{
		// then we don't allow line wins.
		rtnresult = EVT_LOSER;

		// Exit out early.
		return;
	}

	// Keep up with whether or not we have a substitution on the rtnresult.
	bool bSubstitution = false;

	// See what the payout is going left to right.
	// Declare a local variable to track whether or not we found a Viking substitution.
	bool bLeftToRightVikingSubstitution = false;

	// See what the spin result is if we used any Vikings as substitution symbols.
	SpinResult srLeftToRightResult = getLineWinResultWithPossibleSubstitution( stops,
		etLeftToRight, bLeftToRightVikingSubstitution );

	// Now see what the payout is going right to left.
	// Declare a local variable to track whether or not we found a Viking substitution.
	bool bRightToLeftVikingSubstitution = false;

	// See what the spin result is if we used any Vikings as substitution symbols.
	SpinResult srRightToLeftResult = getLineWinResultWithPossibleSubstitution( stops,
		etRightToLeft, bRightToLeftVikingSubstitution );

	// Use the larger payout between the two.
    if ( doesRightToLeftLineWinPayMore( srLeftToRightResult, srRightToLeftResult ) )
	{
		// The right to left win pays more.
		rtnresult = srRightToLeftResult;

		// Use the right to left substitution flag.
		bSubstitution = bRightToLeftVikingSubstitution;

		// Set the right to left line win flag to make things easier for the client.
		setRightToLeftLineWin( line );
	}
	else
	{
		// Otherwise the left to right win pays more.
		rtnresult = srLeftToRightResult;

		// Use the left to right substitution flag.
		bSubstitution = bLeftToRightVikingSubstitution;
	}

	// If we did actually have a substitution and a winning spin...
	if ( bSubstitution && rtnresult != EVT_LOSER )
	{
		// then add a note.
		spinline->addNote( NOTE_VIKING_SUBSTITUTION );
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn		VikingsVoyageGuts::tryForJackpot()
///
///	This function tries to win the jackpot and adds the scatter if we hit
/// the jackpot.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::tryForJackpot( SlotSpin* spin )
{
	// If we are in the first paid spin (NOT a bonus spin)...
	if ( getGameModeId() == MODE_NORMAL ) 
	{
		// then try for the Random Progressive Jackpot
		// If we hit the jackpot...
		if ( getRandomProgressiveJackpotResult( spin->getSpinLineCount() ) || 
			m_bForceRandomJackpot)
		{
			// then add a jackpot scatter to the spin.
			spin->addScatter( 0,
				getPayout( EVT_RANDOM_PROGRESSIVE_WINNER )->payoutAmount, 1,
				true, EVT_RANDOM_PROGRESSIVE_WINNER, 0 );
		}
    }
}

////////////////////////////////////////////////////////////////////////////
/// @fn		VikingsVoyageGuts::getScatterWinResult()
///
/// @param   spin				The spin we are evaluating.
/// @param   et					The direction that we should use when we evaluate the scatter.
/// @param   scatterPositions	An output parameter that contains a bit mask for the icon positions.
///
/// @return	 SpinResult			A payout from the SpinResult enumeration.
///
///	This function looks for scatter wins.  This game supports "left to right"
/// and "right to left".  It also supports "any" during the respin feature.
////////////////////////////////////////////////////////////////////////////
VikingsVoyageGuts::SpinResult VikingsVoyageGuts::getScatterWinResult( SlotSpin* spin, EvalType et, int32& scatterPositions ) const
{
	// Keep up with how many scatter icons we found.
	uint8 numLongboatIcons = 0;

	// Keep up with the direction we are going.
	int nDirection;

	// We need a place to keep up with the starting reel since we can go
	// in either direction.
	int nStartReel;

	// If we are going from left to right or we are paying out on any...
	if ( et == etLeftToRight || et == etAny )
	{
		// then start at reel 0.
		nStartReel = 0;

		// We will need to increment the reel as we iterate through the loop.
		nDirection = 1;
	}
	else
	{
		// Otherwise we must be going from right to left.
		ASSERT( et == etRightToLeft );

		// Start on the last reel.
		nStartReel = getReelCount() - 1;

		// We will need to decrement the reel as we iterate through the loop.
		nDirection = -1;
	}

	// Loop through each reel.
  	for( int i = 0; i < getReelCount(); i++ )
	{
		// Figure out what our current reel is for this iteration.
		int reel = nStartReel + ( i * nDirection );

		// The position bit for the current icon in case it needs to be stored in a mask.
		uint8 position = reel;

		// Keep up with whether or not we found a scatter icon on this reel.
		bool bFoundLongboatOnReel = false;

		// Loop through each row in the wheel house.
		for( int row = 0; row < 3; row++ )
		{
			// Get the current stop.
			uint8 stop = relativeWheelPosition( getGameModeId(), reel,
				spin->getStops()[ reel ], row );

			// Get the icon for the current stop.
			uint8 currentIcon = getIcon( getGameModeId(), reel, stop )->iconid;
			
			// If the icon is a Longboat...
			if ( currentIcon == ICON_LONGBOAT )
			{
				// then increase the Longboat count
				numLongboatIcons++;

				// Set the bit in the position mask for this stop.
				scatterPositions |= 1 << position;

				// Set the flag so that we know we found a scatter icon on this reel.
				bFoundLongboatOnReel = true;
			}

			// Move to the next bit in the position mask.
            position += 5;
		}

		// If we are not paying out for Any and
		// we didn't find a Longboat on the reel...
		if ( et != etAny && !bFoundLongboatOnReel )
		{
			// then we are done.
			break;
		}
	}

	// Use the array to look up the spin result in the current game mode.
	return lookupScatterWinSpinResult( getGameModeId(), numLongboatIcons );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::evaluateScatterWin()
///
/// This function determines if the current spin wins a scatter payment and
/// adds the scatter.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::evaluateScatterWin( SlotSpin* spin, SpinResult srScatterResult, int32 scatterPositions )
{
	// If we got the scatter win (or almost got it)
	if ( srScatterResult != EVT_LOSER )
	{
		// Get the base payout for the scatter.
		int32 nPayout = getPayout( srScatterResult )->payoutAmount;
		
		// Scatters payout based on the total bet, so factor in the number of bet lines.
		nPayout *= spin->getSpinLineCount();

		// Add the payout to the total win for this spin.
		increaseTotalWin( nPayout );
    
		// Add a scatter to the spin.
		spin->addScatter( scatterPositions,
			nPayout * getBetPerLine(), 1, false, srScatterResult, 0 );
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::evaluateFeatureTriggerNormalMode()
///
/// This function determines if the feature is triggered when we are
/// currently in MODE_NORMAL.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::evaluateFeatureTriggerNormalMode( SlotSpin* spin, SpinResult srScatterResult, int32 scatterPositions )
{
	// We need a spin result for the feature trigger.
	SpinResult srFeatureTrigger;

	// We need a place to keep up with how many free spins to award.
	int32 nFreeSpins = 0;

	// If the scatter result was a loser...
	if ( srScatterResult == EVT_LOSER )
	{
		// then we are done so just return now.
		return;
	}
	// Otherwise if the scatter result was a near miss...
	else if ( srScatterResult == EVT_SCATTER_NEAR_MISS )
	{
		// then we will also have a near miss for the feature.
		srFeatureTrigger = EVT_FEATURE_NEAR_MISS;
	}
	// Otherwise...
	else
	{
		// we hit the free game feature.
		srFeatureTrigger = EVT_FREE_GAME_FEATURE;

		// Make sure that we really did have the correct scatter icons.
		ASSERT( srScatterResult == EVT_THREE_LONGBOAT || 
			srScatterResult == EVT_FOUR_LONGBOAT ||
			srScatterResult == EVT_FIVE_LONGBOAT );

		// They always get 10 free games to start with.
		nFreeSpins = 10;

		// Add the free spins.
		incrementFreeSpins( nFreeSpins );

		// Reset the Feature Game Data level.
		m_subGame->update( SGDIDX_CURRENT_FEATURE_LEVEL, 0 );
	}

	// Add a scatter to the spin for the near miss or the feature trigger.
	spin->addScatter( scatterPositions, 0, 0, false,
		srFeatureTrigger, nFreeSpins );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::evaluateFeatureTriggerFreeSpinMode()
///
/// This function determines if the feature is triggered when we are
/// currently in MODE_FREE_SPIN.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::evaluateFeatureTriggerFreeSpinMode( SlotSpin* spin, SpinResult srScatterResult, int32 scatterPositions )
{
	// We need a spin result for the feature trigger.
	SpinResult srFeatureTrigger;

	// We need a place to keep up with how many re-spins to award.
	int32 nRespins = 0;

	// If the scatter result was a loser...
	if ( srScatterResult == EVT_LOSER )
	{
		// then we are done so just return now.
		return;
	}
	// Otherwise if the scatter result was a near miss...
	else if ( srScatterResult == EVT_SCATTER_NEAR_MISS )
	{
		// then we will also have a near miss for the feature.
		srFeatureTrigger = EVT_FEATURE_NEAR_MISS;
	}
	// Otherwise...
	else
	{
		// we hit the re-spin feature.
		srFeatureTrigger = EVT_RESPIN_FEATURE;

		// Make sure that we really did have the correct scatter icons.
		ASSERT( srScatterResult == EVT_TWO_LONGBOAT ||
			srScatterResult == EVT_THREE_LONGBOAT || 
			srScatterResult == EVT_FOUR_LONGBOAT ||
			srScatterResult == EVT_FIVE_LONGBOAT );

		// The number of re-spins is selected from the probability table.
		nRespins = getRandomValueFromProbabilityTable();

		// Add the re-spins.
		incrementRespins( nRespins );

		// Update Feature Game Data level for an audit trail.
		incrementCurrentFeatureLevel();

		// We need to see which reels had longboats on them.
		// This array of masks will extract the scatter position
		// bits for the given reel.  So to see if reel 2 had any
		// longboats on it, just do this:
		//   scatterPositions & nScatterMask[ 2 ]
		int32 nScatterMask[] = 
		{
			SCATTER_REEL_0_MASK,
			SCATTER_REEL_1_MASK,
			SCATTER_REEL_2_MASK,
			SCATTER_REEL_3_MASK,
			SCATTER_REEL_4_MASK
		};

		// Make sure we have an entry in the scatter mask array
		// for each reel in the game.
		ASSERT( countof( nScatterMask ) == getReelCount() );

		// Get the stops for the spin.
        const uint8* pStops = spin->getStops();

		// Only the triggering reels can be held.  The trigger is
		// a left to right scatter payout, so once we find a reel
		// that does not have a longboat on it we can't hold any
		// more reels after that, even if they do have a longboat
		// on them.
		bool bCanHold = true;

		// Loop through all of the reels.
		for ( uint8 n = 0; n < getReelCount(); n++ )
		{
			// Initialize the stop position for this reel to -1,
			// which is not a valid position and is used to indicate
			// that the reel is not held.
			int32 nStop = -1;

			// If this reel qualifies to be held and it his a longboat icon on it...
			if ( bCanHold && ( scatterPositions & nScatterMask[ n ] ) )
			{
				// Then set the hold stop to be the current stop for the reel.
				nStop = pStops[ n ];
			}
			// Otherwise...
			else
			{
				// None of the remaining reels can be held, even if they do
				// have a longboat on them.
				bCanHold = false;
			}

			// Now set the stop (if any) to hold this reel on during the re-spins.
			setReelHoldStop( n, nStop );
		}
	}

	// Add a scatter to the spin for the near miss or the feature trigger.
	spin->addScatter( scatterPositions, 0, 0, false,
		srFeatureTrigger, nRespins );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::evaluateFeatureTriggerRespinMode()
///
/// This function determines if the feature is triggered when we are
/// currently in MODE_RESPIN.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::evaluateFeatureTriggerRespinMode( SlotSpin* spin, SpinResult srScatterResult, int32 scatterPositions )
{
	// We need a spin result for the feature trigger.
	SpinResult srFeatureTrigger;

	// We need a place to keep up with how many additional free spins to award.
	int32 nFreeSpins = 0;

	// If the scatter was for two longboats...
	if ( srScatterResult == EVT_TWO_LONGBOAT )
	{
		// then we will have a near miss for the feature.
		srFeatureTrigger = EVT_FEATURE_NEAR_MISS;
	}
	// Otherwise if the scatter was for three longboats...
	else if ( srScatterResult == EVT_THREE_LONGBOAT )
	{
		// then we will have a near miss for the feature.
		srFeatureTrigger = EVT_FEATURE_NEAR_MISS;
	}
	// Otherwise if the scatter was for four longboats...
	else if ( srScatterResult == EVT_FOUR_LONGBOAT )
	{
		// then they get 3 more free spins.
		nFreeSpins = 3;

		// Add the free spins.
		incrementFreeSpins( nFreeSpins );

		// we hit the free game feature.
		srFeatureTrigger = EVT_FREE_GAME_FEATURE;
	}
	// Otherwise if the scatter was for five longboats...
	else if ( srScatterResult == EVT_FIVE_LONGBOAT )
	{
		// then they get 5 more free spins.
		nFreeSpins = 5;

		// Add the free spins.
		incrementFreeSpins( nFreeSpins );

		// we hit the free game feature.
		srFeatureTrigger = EVT_FREE_GAME_FEATURE;
	}
	// Otherwise...
	else
	{
		// ASSERT because this should never happen.
		ASSERT( false );
		return;
	}

	// Add a scatter to the spin for the feature trigger or near miss.
	spin->addScatter( scatterPositions, 0, 0, false,
		srFeatureTrigger, nFreeSpins );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::classifyScatters( SlotSpin* spin )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This is an override of the framework method.  
/// It determines if the current spin merits free spins or scatter payments.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::classifyScatters( SlotSpin* spin )
{	
	// Try for the Random Progressive Jackpot
	tryForJackpot( spin );

	// See what the payout is going left to right.
	// Declare a local variable to track the scatter positions.
	int32 nLeftToRightScatterPositions = 0;

	// See what the spin result is going left to right.
	SpinResult srLeftToRightResult = getScatterWinResult( spin,
		etLeftToRight, nLeftToRightScatterPositions );

	// Now see what the payout is going right to left.
	// Declare a local variable to track the scatter positions.
	int32 nRightToLeftScatterPositions = 0;

	// See what the spin result is going right to left.
	SpinResult srRightToLeftResult = getScatterWinResult( spin,
		etRightToLeft, nRightToLeftScatterPositions );

	// Now see what the payout is for Any.
	// Declare a local variable to track the scatter positions.
	int32 nAnyScatterPositions = 0;

	// See what the spin result is.
	SpinResult srAnyResult = getScatterWinResult( spin,
		etAny, nAnyScatterPositions );

	// If we are in normal mode...
	if ( getGameModeId() == MODE_NORMAL )
	{
		// then scatters payout left to right and right to left.
		evaluateScatterWin( spin, srLeftToRightResult, nLeftToRightScatterPositions );

		// If the left to right result was not five longboat...
		if ( srLeftToRightResult != EVT_FIVE_LONGBOAT )
		{
			// then also give them the right to left scatter.
			evaluateScatterWin( spin, srRightToLeftResult, nRightToLeftScatterPositions );
		}

		// Now see if they triggered the feature.  Only the left to right scatter can
		// be a feature trigger.
		evaluateFeatureTriggerNormalMode( spin, srLeftToRightResult, nLeftToRightScatterPositions );
	}
	// Otherwise if we are in free spin mode...
	else if ( getGameModeId() == MODE_FREE_SPIN )
	{
		// then scatters payout left to right and right to left.
		evaluateScatterWin( spin, srLeftToRightResult, nLeftToRightScatterPositions );

		// If the left to right result was not five longboat...
		if ( srLeftToRightResult != EVT_FIVE_LONGBOAT )
		{
			// then also give them the right to left scatter.
			evaluateScatterWin( spin, srRightToLeftResult, nRightToLeftScatterPositions );
		}

		// Now see if they triggered the feature.  Only the left to right scatter can
		// be a feature trigger.
		evaluateFeatureTriggerFreeSpinMode( spin, srLeftToRightResult, nLeftToRightScatterPositions );
	}
	// Otherwise...
	else
	{
		// they must be in re-spin mode...
		ASSERT( getGameModeId() == MODE_RESPIN );

		// In this mode scatters payout any.
		evaluateScatterWin( spin, srAnyResult, nAnyScatterPositions );

		// Now see if they triggered the feature.  In this mode the free game feature is
		// re-triggered with ANY 4 or 5 scatter.
		evaluateFeatureTriggerRespinMode( spin, srAnyResult, nAnyScatterPositions );
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::setRightToLeftLineWin()
///
/// This function sets the right to left line win flag for the passed in
/// line.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::setRightToLeftLineWin( uint8 nLine )
{
	// Get the current flags.
	int32 n = m_subGame->getData( SGDIDX_RIGHT_TO_LEFT_LINE_WINS );

	// Turn on the flag for the new line.
	n |= 1 << nLine;

	// Store the new flags in the sub-game data.
    m_subGame->update( SGDIDX_RIGHT_TO_LEFT_LINE_WINS, n );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::resetRightToLeftLineWins()
///
/// This function resets all of the right to left line win flags in
/// preparation for a new spin.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::resetRightToLeftLineWins()
{
	// Just set the sub-game data back to zero.
    m_subGame->update( SGDIDX_RIGHT_TO_LEFT_LINE_WINS, 0 );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::incrementCurrentFeatureLevel()
///
/// This function updates the feature level sub-game data and moves us
/// to the next feature level.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::incrementCurrentFeatureLevel()
{
	// Get the current feature level.
	int32 n = m_subGame->getData( SGDIDX_CURRENT_FEATURE_LEVEL );

	// Set the feature to that level.
	m_feature->setLevel( n );

	// Now increment the sub-game data so that we will remember the new value.
    m_subGame->update( SGDIDX_CURRENT_FEATURE_LEVEL, n + 1 );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::getRespins()
///
/// Accessor to get the number of re-spins from the sub-game data.
////////////////////////////////////////////////////////////////////////////
int32 VikingsVoyageGuts::getRespins()
{
    return m_subGame->getData( SGDIDX_RESPINS_REMAINING );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::incrementRespins()
///
/// Helper function used to award more respins.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::incrementRespins(int32 numSpins /* = 1 */ )
{
	// See how many re-spins they already have.
	int32 numRespinsRemaining = getRespins();

	// Add in the new re-spins and store it in the sub-game data.
	m_subGame->update( SGDIDX_RESPINS_REMAINING, numRespinsRemaining + numSpins );

	// Also include these as free spins so that we don't exit the feature too
	// early if we get re-spins on our last free spin..
	incrementFreeSpins( numSpins );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::decrementFreeSpins()
///
/// This is an override of the framework function.  It is used for both
/// free spins and re-spins, since the framework is not set up to handle
/// two different free spin counters at the same time.  However, that problem
/// is easily solved by making our version of the function smart enough
/// to pick the appropriate counter to decrement based on the game mode we
/// are in at the time.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::decrementFreeSpins(int32 numSpins /*= 1*/)
{
	// If we are in re-spin mode...
	if ( getGameModeId() == MODE_RESPIN )
	{
		// then see how many re-spins they have right now.
		int32 numRespinsRemaining = getRespins();

		// Do a simple bounds check.
		if ( numRespinsRemaining >= numSpins )
		{
			// It is within bounds so just update the sub-game data with the new value.
			m_subGame->update( SGDIDX_RESPINS_REMAINING, numRespinsRemaining - numSpins );
		}
		// Otherwise...
		else
		{
			// break into the debugger to see what went wrong.
			ASSERT( false );

			// Just remove all of the remaining free spins and re-spins.
			removeAllFreeSpins();
		}
	}

	// Now just call the base class to decrement the normal counter
	// that it already knows about.
	SlotGuts::decrementFreeSpins( numSpins );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::removeAllFreeSpins()
///
/// This is an override of the framework function.  It is used for both
/// free spins and re-spins, since the framework is not set up to handle
/// to different free spin counters at the same time.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::removeAllFreeSpins()
{
	// First call the base class to remove all of the free spins.
	SlotGuts::removeAllFreeSpins();

	// Now update our sub-game data to get rid of the re-spins.
    m_subGame->update( SGDIDX_RESPINS_REMAINING, 0 );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::isValidGuts()   
///
/// @return	 bool8      True if gut is valid.    
///
/// This helper method is used to see if the current gut ID is valid.
////////////////////////////////////////////////////////////////////////////
bool8 VikingsVoyageGuts::isValidGuts()
{
	// Make sure that the m_aLineWinSpinResults array has an entry for each reel,
	// and an additional entry in case there are 0 icons on the line.
	// Note:  we can't do this in the constructor because we have to call
	// getReelCount.
	ASSERT( countof( m_aLineWinSpinResults[0] ) == getReelCount( MODE_NORMAL ) + 1 );

	// Make sure that the m_aScatterWinSpinResults array has an entry for each reel,
	// and an additional entry in case there are 0 scatter icons.
	ASSERT( countof( m_aScatterWinSpinResults[ MODE_NORMAL ] ) == getReelCount( MODE_NORMAL ) + 1 );

	// Now make sure that all game modes have the same number of reels.
	ASSERT( getReelCount( MODE_NORMAL ) == getReelCount( MODE_FREE_SPIN ) );
	ASSERT( getReelCount( MODE_NORMAL ) == getReelCount( MODE_RESPIN ) );

	// Finally just check to see if the gut is within the right range.
    return ( getGutsId() >= 800 && getGutsId() < 900 );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::getGameModeMultiplierAndNoteId( uint8& nGameModeMultiplier, uint8& nNoteId )
///
/// @param   nGameModeMultiplier	The multiplier for the game mode.
/// @param   nNoteId				The note ID for the game mode.
///
/// This helper method is used to get the game mode multiplier and note ID.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::getGameModeMultiplierAndNoteId( uint8& nGameModeMultiplier,
	uint8& nNoteId )
{
	// If we are in free spin mode or the respin mode...
    if ( getGameModeId() == MODE_FREE_SPIN || getGameModeId() == MODE_RESPIN )
    {
		// then return the free spin multiplier...
        nGameModeMultiplier = MULTIPLIER_FREE_SPIN;

		// and the free spin note.
        nNoteId = NOTE_FREE_SPIN;
    }
	// Otherwise...
    else
    {
		// just defer to the base class.
        SlotGuts::getGameModeMultiplierAndNoteId( nGameModeMultiplier, nNoteId );
    }
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::getReelHoldStop()
///
/// Accessor function to get the stop position to hold for the passed in
/// reel.
////////////////////////////////////////////////////////////////////////////
int32 VikingsVoyageGuts::getReelHoldStop( uint8 nReel )
{
	// Simple bounds check.
	ASSERT( nReel < getReelCount() );

	// Just return the sub-game data for the reel.
    return m_subGame->getData( SGDIDX_REEL_HOLD_STOP_1 + nReel );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::setReelHoldStop()
///
/// Accessor function to set the stop position to hold for the passed in
/// reel.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::setReelHoldStop( uint8 nReel, int32 nStop )
{
	// Simple bounds check.
	ASSERT( nReel < getReelCount() );

	// Update the sub-game data.
    m_subGame->update( SGDIDX_REEL_HOLD_STOP_1 + nReel, nStop );

	// If we are holding this reel...
	if ( nStop != -1 )
	{
		// then log it in the feature data for an audit trail.
		m_feature->update( SGDIDX_REEL_HOLD_STOP_1 + nReel, nStop, true );
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::preSpinClassifyResults( SlotSpin* spin )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This method is overridden to alter the reel stops for the respin feature.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::preSpinClassifyResults( SlotSpin* spin )
{
	SlotGuts::preSpinClassifyResults(spin);

	// Reset all of the right to left line win flags.
	resetRightToLeftLineWins();

	// If we are in the re-spin feature...
    if ( getGameModeId() == MODE_RESPIN )
    {
		// then we will need to hold some of the reel stops, which
		// means we need to cast away the constness of the stops.
        uint8* pStops = const_cast< uint8* >( spin->getStops() );
		
		// An extra check in debug builds.
		#ifdef _DEBUG
			uint8 nReelsHeld = 0;
		#endif

		// Loop through all of the reels.
		for ( uint8 n = 0; n < getReelCount(); n++ )
		{
			// Get the stop we are supposed to use for this reel, if any.
			int32 nStop = getReelHoldStop( n );

			// If we are holding this reel at a specific stop...
			if ( nStop != -1 )
			{
				// then store the stop in the spin.
				pStops[ n ] = nStop;

				// An extra check in debug builds.
				#ifdef _DEBUG
					nReelsHeld++;
				#endif
			}
			// Otherwise...
			else
			{
				// break out of the loop since none of the remaining reels
				// can be held.  The reels that are held are triggered by a
				// left to right scatter, so once we find one that is not
				// held then we know the rest of them aren't held either.
				break;
			}
		}

		// Make sure that we found at least 2 reels that are held.
		ASSERT( nReelsHeld >= 2 );

		// Sanity check.
		ASSERT( nReelsHeld <= getReelCount() );
    }
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::postSpinClassifyResults( SlotSpin* spin )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This method is overridden to change the game mode if necessary.
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::postSpinClassifyResults( SlotSpin* spin )
{
	// If we have re-spins...
	if ( getRespins() > 0 )
	{
		// then make sure we are in re-spin mode.
		setGameModeId( MODE_RESPIN );
	}
	// Otherwise if we have free spins...
	else if ( getFreeSpins() > 0 )
	{
		// then make sure we are in free spin mode.
		setGameModeId( MODE_FREE_SPIN );
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VikingsVoyageGuts::Debug_SetLoserStops( uint8* pStops )
///
/// @param   pStops		The stops to set.
///
/// Base class override, returns a losing spin triggered by the debug tool
////////////////////////////////////////////////////////////////////////////
void VikingsVoyageGuts::Debug_SetLoserStops( uint8* pStops )
{
	pStops[0]=0;
	pStops[1]=0;
	pStops[2]=0;
	pStops[3]=0;
	pStops[4]=3;
}